<template>
  <lb-layout
    v-loading="$store.getters.loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="#a19f9fb0;"
    :paddBottom="5"
  >
    <aps-form
      :config="apsForm.formConfig"
      :model="apsForm.formData"
      :formRef="apsForm.formRef"
      :labelWidth="apsForm.labelWidth"
      ref="form"
    ></aps-form>
    <lb-table
      :column="tableData.column"
      :data="tableData.data"
      border
      :height="600"
      pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :page-sizes="[20, 40, 60, 80]"
      :pager-count="5"
      :current-page.sync="currentPage"
      :total="total"
      :page-size="pageSize"
      @size-change="handleSizeChange"
      @p-current-change="handleCurrentChange"
    >
    </lb-table>
    <component
      :is="modelName"
      :id="id"
      :visible.sync="show"
      @close="close"
    ></component>
  </lb-layout>
</template>

  <script>
import { list, del } from "@/api/configuration/energyUnitPrice.js";
import CascaderSelect from "@/components/Select/CascaderSelect";
import operate from "@/views/configuration/energyUnitPrice/components/operate";
import detail from "@/views/configuration/energyUnitPrice/components/detail";
import auth from "@/plugins/auth";
export default {
  name: "energyUnitPrice",
  components: {
    operate,
    detail,
    CascaderSelect,
  },
  data() {
    return {
      //弹窗
      id: "",
      modelName: "",
      show: false,
      apsForm: {
        formRef: "form",
        labelWidth: "auto",
        formData: {},
        formConfig: {
          formAttr: {
            inline: true,
            size: "small",
          },
          properties: {
            deptId: {
              label: "适用区域:",
              render: (h) => {
                let that = this;
                let valNum = that.$refs?.form?.getFieldsValue().deptId;
                let a = (
                  <CascaderSelect
                    values={valNum}
                    value={valNum}
                    requestMethod={"get"}
                    varient={"areaDept"}
                    size={"small"}
                    placeholder={"请选择区域"}
                    onInput={(value) => {
                      if (value && value.length > 0) {
                        this.$refs?.form?.setFieldValue(
                          "deptId",
                          value[value.length - 1]
                        );
                      }
                    }}
                  ></CascaderSelect>
                );
                return <div>{a}</div>;
              },
            },
            date: {
              label: "适用周期:",
              typeName: "el-date-picker",
              type: "monthrange",
              size: "small",
              "start-placeholder": "开始日期",
              "end-placeholder": "结束日期",
              "value-format": "yyyy-MM-dd HH:mm:ss",
            },
          },
          formSoltButton: [
            {
              label: "搜索",
              type: "primary",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-search",
              click: this.handlerSearchDetail,
            },
            {
              label: "重置",
              typeName: "el-button",
              size: "small",
              icon: "el-icon-refresh-left",
              click: this.handleReset,
            },
            {
              label: "新增",
              type: "primary",
              typeName: "el-button",
              display: true,
              plain: true,
              size: "small",
              icon: "el-icon-plus",
              click: () => this.showModel("operate"),
            },
          ],
        },
      },
      currentPage: 1,
      pageSize: 20,
      total: 0,
      tableData: {
        column: [
          {
            prop: "deptName",
            label: "适用区域",
            minWidth: 100,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "effectivePeriod",
            label: "适用周期",
            minWidth: 200,
            align: "center",
            showOverflowTooltip: true,
          },
          {
            prop: "updateTime",
            label: "最后更新时间",
            minWidth: 200,
            align: "center",
          },
          {
            prop: "userName",
            label: "更新人",
            minWidth: 120,
            align: "center",
          },
          {
            prop: "phoneNum",
            label: "联系电话",
            minWidth: 120,
            align: "center",
          },
          {
            label: "操作",
            width: 210,
            fixed: "right",
            render: (h, scope) => {
              let that = this;
              let a = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-view"
                  v-hasPermi={["energy:budget-declare-type-config:edit"]}
                  onClick={() => {
                    this.showModel("detail", scope.row);
                  }}
                >
                  详情
                </el-button>
              );
              let b = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-edit"
                  v-hasPermi={["energy:budget-declare-type-config:edit"]}
                  onClick={() => {
                    this.showModel("operate", scope.row);
                  }}
                >
                  编辑
                </el-button>
              );
              let c = (
                <el-button
                  type="text"
                  size="small"
                  icon="el-icon-delete"
                  v-hasPermi={["energy:budget-declare-type-config:remove"]}
                  onClick={() => {
                    this.handleOperDelete(scope.row);
                  }}
                  class="text-danger"
                >
                  删除
                </el-button>
              );
              return (
                <div>
                  {b}
                  {a}
                  {c}
                </div>
              );
            },
          },
        ],
        data: [],
      },
    };
  },
  mounted() {
    this.judgePermi();
    this.handlerSearchDetail();
  },
  methods: {
    judgePermi() {
      if (auth.hasPermi("energy:budget-declare-type-config:add")) {
        this.apsForm.formConfig.formSoltButton[2].display = false;
      }
    },
    async initData(data) {
      let that = this;
      let query = {
        ...data,
      };
      query.pageNum = that.currentPage;
      query.pageSize = that.pageSize;
      that.$store.commit("app/updateApiCount", "add");
      await list(query)
        .then((res) => {
          that.$store.commit("app/updateApiCount");
          if (res.code === 700) {
            this.$message.warning(res.msg);
            return false;
          }
          that.tableData.data = res?.rows ?? [];
          that.total = res?.total ?? 0;
        })
        .catch((err) => {
          that.$store.commit("app/updateApiCount");
          // this.$message.error("系统错误,请联系管理员!错误码317001");
        });
    },
    // 搜索
    handlerSearchDetail() {
      const { date, ...other } = this.$refs?.form?.getFieldsValue();
      const [effectiveTime, expirationTime] = date || [];
      const data = {
        ...other,
        effectiveTime,
        expirationTime,
      };
      this.initData(data);
    },
    // 重置
    handleReset() {
      this.$refs?.form?.resetFields();
      this.handlerSearchDetail();
    },
    handleSizeChange(val) {
      this.currentPage = 1;
      this.pageSize = val;
      this.handlerSearchDetail();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handlerSearchDetail();
    }, //删除
    handleOperDelete(row) {
      let that = this;
      const text = "是否确认删除" + (row?.name ?? "该条数据") + "？";
      this.$modal
        .confirm(text)
        .then(() => {
          that.$store.commit("app/updateApiCount", "add");
          del(row.id)
            .then((res) => {
              that.$store.commit("app/updateApiCount");
              if (res.code === 700) {
                that.$message.warning(res.msg);
                return false;
              }
              that.$message.success("已删除!");
              that.handlerSearchDetail();
            })
            .catch((err) => {
              that.$store.commit("app/updateApiCount");
              // this.$message.error("系统错误,请联系管理员!317005");
            });
        })
        .catch(() => {});
    },
    // 弹窗
    showModel(name, params) {
      this.modelName = name;
      this.id = params?.id || "";
      setTimeout(() => {
        this.show = true;
      }, 200);
    },
    close(status) {
      this.show = false;
      setTimeout(() => {
        if (status) this.handlerSearchDetail();
      }, 200);
    },
  },
};
</script>
